//
//  AudioTableViewCell.m
//  RecorderTest
//
//  Created by 张彦东 on 2017/5/17.
//  Copyright © 2017年 Z. All rights reserved.
//

#import "AudioTableViewCell.h"
#import "ZZAudioModel.h"
#import "ZZAudioManager.h"

@interface AudioTableViewCell ()

@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UILabel *sizeLabel;
@property (nonatomic, weak) UIButton *playButton;
@property (nonatomic, weak) UIButton *stopButton;

@end

@implementation AudioTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    NSString *className = NSStringFromClass([self class]);
    [tableView registerClass:[self class] forCellReuseIdentifier:className];
    return [tableView dequeueReusableCellWithIdentifier:className];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setup];
    }
    return self;
}

- (void)setup {
    UILabel *name = [[UILabel alloc] init];
    [self addSubview:name];
    _nameLabel = name;
    name.font = [UIFont systemFontOfSize:15];
    name.textColor = [UIColor blackColor];
    
    UILabel *time = [[UILabel alloc] init];
    [self addSubview:time];
    _timeLabel = time;
    time.font = [UIFont systemFontOfSize:13];
    time.textColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.8];
    
    UILabel *size = [[UILabel alloc] init];
    [self addSubview:size];
    _sizeLabel = size;
    size.font = [UIFont systemFontOfSize:13];
    size.textColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.8];
    
    UIButton *play = [UIButton buttonWithType:UIButtonTypeSystem];
    [self addSubview:play];
    _playButton = play;
    [play setTitle:@"play" forState:UIControlStateNormal];
    [play setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [play setTitleColor:[[UIColor blueColor] colorWithAlphaComponent:0.5] forState:UIControlStateDisabled];
    [play addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *stop = [UIButton buttonWithType:UIButtonTypeSystem];
    [self addSubview:stop];
    _stopButton = stop;
    [stop setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [stop setTitle:@"stop" forState:UIControlStateNormal];
    [stop addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioPlayComplete:) name:ZZAudioStopNotification object:nil];
}

- (void)audioPlayComplete:(NSNotification *)notification {
    NSInteger audioId = [notification.object integerValue];
    if (_model.audioId.integerValue != audioId) return;
    if (_stopButton.isHidden) return;
    [self changeButtonStatusIsPlaying:NO];
}

- (void)stop {
    [[ZZAudioManager manager] audioStopWithAudioId:_model.audioId];
    [self changeButtonStatusIsPlaying:NO];
}

- (void)play {
    [[ZZAudioManager manager] audioPlayWithAudioId:_model.audioId];
    [self changeButtonStatusIsPlaying:YES];
}

- (void)changeButtonStatusIsPlaying:(BOOL)isPlaying {
    if (isPlaying) {
        _stopButton.hidden = NO;
        _playButton.enabled = NO;
    } else {
        _stopButton.hidden = YES;
        _playButton.enabled = YES;
    }
}

- (void)setModel:(ZZAudioModel *)model {
    _model = model;
    
    _nameLabel.text = model.name;
    _timeLabel.text = model.durationDisp;
    _sizeLabel.text = model.fileSizeDisp;
    [self changeButtonStatusIsPlaying:NO];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat viewWidth = self.frame.size.width;
    CGFloat viewHeight = self.frame.size.height;
    
    CGFloat buttonWidth = 80;
    CGFloat nameWidth = viewWidth - 30;
    _playButton.frame = CGRectMake(viewWidth - buttonWidth, 0, buttonWidth, viewHeight);
    _stopButton.frame = CGRectMake(viewWidth - buttonWidth * 2, 0, buttonWidth, viewHeight);
    _nameLabel.frame = CGRectMake(15, 0, nameWidth, viewHeight / 2);
    CGFloat infoHeight = (viewHeight - CGRectGetHeight(_nameLabel.frame)) / 2;
    CGFloat infoWidth = viewWidth - buttonWidth * 2 - 10 - 5;
    _timeLabel.frame = CGRectMake(15, CGRectGetMaxY(_nameLabel.frame), infoWidth, infoHeight);
    _sizeLabel.frame = CGRectMake(15, CGRectGetMaxY(_timeLabel.frame), infoWidth, infoHeight);
}

@end
